import { hot_search_info, search_music, search_detail } from "@/network/api";
import http from "@/network/http";

export default {
  namespaced: true,
  state: {
    hotWord: [], //热门搜索词
    searchList: [], //搜索结果
    songs:[],
    loading:false
  },
  mutations: {
    setHotWord(state, payload) {
      state.hotWord = payload;
    },
    setSearchList(state, payload) {
      state.searchList = payload;
    },
    setSongs(state,payload){
      state.songs=payload
    },
    setLoading(state,payload){
      state.loading=payload
    }
  },
  actions: {
    async getHotWord(context) {
      let {
        result: { hots },
      } = await http.get(hot_search_info);
      context.commit("setHotWord", hots);
    },
    async getSearchList(context, payload) {
      context.commit('setLoading',true)
      let {
        result: { songs },
      } = await http.get(search_music, payload);
      songs = songs.map((item) => ({
        id: item.id, //id
        title: item.name, //歌名
        ar: item.artists.map((arItem) => arItem.name), //歌手
        al: {
          //专辑
          name: item.album.name,
          picUrl: "",
        },
      }));
      context.commit('setLoading',false)
      context.commit("setSearchList", songs);
    },
    // 获取查询的当前歌曲信息
    async getSearchDetail(context, payload) {
      let { songs } = await http.get(search_detail, payload);
      songs = songs.map((item) => ({
        id: item.id, //id
        title: item.name, //歌名
        ar: item.ar.map((arItem) => arItem.name), //歌手
        al: {
          //专辑
          name: item.al.name,
          picUrl: item.al.picUrl,
        }
      }))
      context.commit('player/setPlaySongList',songs,{root:true})
      context.commit('player/setCurrentIndex',0,{root:true})
    },
  },
};
